<?php
$page_title = 'Thiệp 8-3';
include 'header.inc';

function generate_page_links($cur_page, $num_pages) {
    $page_links = '';

    // If this page is not the first page, generate the "previous" link
    if ($cur_page > 1) {
        $page_links .= '<a href="' . $_SERVER['PHP_SELF'] . '?page=' . ($cur_page - 1) . '"><img src="images/back_arrow.png"></a> ';
    } else {
        $page_links .= '<img src="images/back_arrow.png"> ';
    }

    // Loop through the pages generating the page number links
    for ($i = 1; $i <= $num_pages; $i++) {
        if ($cur_page == $i) {
            $page_links .= ' ' . $i;
        } else {
            $page_links .= ' <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $i . '"> ' . $i . '</a>';
        }
    }

    // If this page is not the last page, generate the "next" link
    if ($cur_page < $num_pages) {
        $page_links .= ' <a href="' . $_SERVER['PHP_SELF'] . '?page=' . ($cur_page + 1) . '"><img src="images/for_arrow.png"></a>';
    } else {
        $page_links .= ' <img src="images/for_arrow.png">';
    }

    return $page_links;
}
?>
<div id="mainContent">
    <?php
    include 'leftPanel.inc';
    include 'rightPanel.inc';
    ?>

    <div id="middleContent">

        <div id="sanpham" class="text">
            <?php
// Calculate pagination information
            $cur_page = isset($_GET['page']) ? $_GET['page'] : 1;
            $results_per_page = 12;
            $skip = (($cur_page - 1) * $results_per_page);
            
            require_once 'connectvars.php';
// Connect to the database 
            $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
            mysqli_set_charset($dbc, "utf8");
            $query = "SELECT * FROM tbl_thiep ";
            $data = mysqli_query($dbc, $query);
            $total = mysqli_num_rows($data);
            $num_pages = ceil($total / $results_per_page);
            
            $query =  $query . " LIMIT $skip, $results_per_page";
            $data = mysqli_query($dbc, $query);


            echo '<table width="100%" border="0" cellspacing="5" cellpadding="5">';

            $i = 0;
            while ($row = mysqli_fetch_array($data)) {
                if ($i == 0) {
                    echo '<tr>';
                }
                echo '<td>';
                echo '<img src="' . $row['thiep_hinh'] . '" height="100" width="180"><br>';
                echo '<p align="center"><b><a href="guiThiep.php?thiepID='.$row['thiep_id'].'">' . $row['thiep_ten'] . '</a></b></p>';
                echo '</td>';
                $i++;
                if ($i == 3) {
                    echo '</tr>';
                    $i = 0;
                }
            }
            if ($num_pages > 1) {
                echo '<tr><td><font size="5"><b>';
                echo generate_page_links($cur_page, $num_pages);
                echo '</b></font></td></tr>';
            }
            ?>
            </table>
        </div>

    </div>
</div>
<?php
include 'footer.inc';
?>
